<?php

class DBC {

    static $db = "normal"; // set $db = "normal" when using local connection; for sina application engine set $db = "sae";

    function __construct() {
        if (self::$db == "normal") {
//	mysql_connect ( "localhost", "root", "root" ) or die ( "Could not connect: " . mysql_error () );
            mysql_connect('shuasc.gotoftp3.com', 'shuasc', 'shuasc2012') or die("Could not connect: " . mysql_error());
            mysql_select_db("shuasc");
            mysql_query("SET NAMES 'gbk';");
        }
    }

    public function fetchData($sql) {
        if (self::$db == "normal") {
            return $this->fetchDataNormal($sql);
        } else if (self::$db == "sae") {
            return $this->fetchDataSae($sql);
        }
    }

    private function fetchDataNormal($sql) {
        $result = mysql_query($sql);

        if ($result != false) {
            $data = array();
            $i = 0;
            while (($row = mysql_fetch_array($result, MYSQL_ASSOC)) != false) {
                $data [$i] = $row;
                $i++;
            }
            mysql_free_result($result);
            return $data;
        }
        return $result;
    }

    private function fetchDataSae($sql) {
        $mysql = new SaeMysql ();
        $data = $mysql->getData($sql);
        $mysql->closeDb();
        if (is_null($data)) {
            return array();
        }
        return $data;
    }

    public function insertId($sql) {
        if (self::$db == "normal") {
            return $this->insertIdNormal($sql);
        } else if (self::$db == "sae") {
            return $this->insertIdSae($sql);
        }
    }

    private function insertIdNormal($sql) {
        $result = mysql_query($sql);
        if ($result) {
            return mysql_insert_id();
        }
        return $result;
    }

    private function insertIdSae($sql) {
        $mysql = new SaeMysql ();
        if ($mysql->runSql($sql)) {
            $result = $mysql->lastId();
        } else {
            $result = false;
        }
        $mysql->closeDb();
        return $result;
    }

    public function execute($sql) {
        if (self::$db == "normal") {
            return $this->executeNormal($sql);
        } else if (self::$db == "sae") {
            return $this->executeSae($sql);
        }
    }

    private function executeNormal($sql) {
        return mysql_query($sql);
    }

    private function executeSae($sql) {
        $mysql = new SaeMysql ();
        $result = $mysql->runSql($sql);
        $mysql->closeDb();
        return $result;
    }

    public function getRows($sql) {
        if (self::$db == "normal") {
            return $this->getRowsNormal($sql);
        } else if (self::$db == "sae") {
            return $this->getRowsSae($sql);
        }
    }

    private function getRowsNormal($sql) {
        $result = mysql_query($sql);
        if ($result != false) {
            return mysql_num_rows($result);
        }
        return $result;
    }

    private function getRowsSae($sql) {
        $mysql = new SaeMysql ();
        $data = array();
        $data = $mysql->getData($sql);
        $mysql->closeDb();
        if (is_null($data)) {
            return 0;
        } else if (is_array($data)) {
            return count($data);
        }
        return 0;
    }

}

?>
